//
//  PickerViewController.m
//  MKUITestApp
//
//  Created by sushangjin on 2020/6/5.
//  Copyright © 2020 sushangjin. All rights reserved.
//

#import "PickerViewController.h"
#import "ItemPickerViewCellModel.h"
#import "ItemPickerViewCellModel.h"
#import "ItemPickerViewCell.h"
@interface PickerViewController ()
@property(nonatomic,strong) MKUIPickerCycleView *pickerView;
@property(nonatomic,strong) MKUIPickerViewModel *model;
@property(nonatomic,strong) UILabel *infoLabel;
@end

@implementation PickerViewController

+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"PickerViewControllerPickerViewControllerPickerViewController" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pickerView = [[MKUIPickerCycleView alloc] init];
    [self.view addSubview:self.pickerView];
    self.model = [[MKUIPickerViewModel alloc] initWithPickerView:self.pickerView];
    //
    self.infoLabel = [[UILabel alloc] init];
    self.infoLabel.numberOfLines = 0;
    [self.view addSubview:self.infoLabel];
    [self __reloadData];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.safeBounds;
    CGRect f1 = bounds;
    f1.size = [self.infoLabel sizeThatFits:bounds.size];
    f1.origin.y = CGRectGetMaxY(bounds)-f1.size.height;
    self.infoLabel.frame = f1;
    //
    CGRect f2 = bounds;
    f2.size.height = f1.origin.y-f2.origin.y;
    self.pickerView.frame = f2;
}
- (void)__refreshSelect{
    NSMutableArray<NSString *> *infos = [[NSMutableArray alloc] initWithCapacity:self.pickerView.numberOfComponents];
    for (int i=0; i<self.pickerView.numberOfComponents; i++) {
        NSInteger r = [self.pickerView selectedRowInComponent:i];
        ItemPickerViewCellModel *cm = [self.model cellModelAtIndexPath:[NSIndexPath indexPathForRow:r inSection:i]];
        [infos addObject:[NSString stringWithFormat:@"%@(%@)",cm.title,@(r)]];
    }
    self.infoLabel.text = [NSString stringWithFormat:@"select:[%@]",[infos componentsJoinedByString:@","]];
    [self.view setNeedsLayout];
    [self.pickerView reloadAllComponents];
}
- (void)__reloadData{
    [self.model removeAllSectionModels];
    
    [self.model addSectionModel:[[MKUIPickerViewComponentModel alloc] init]];
    for (NSString *name in @[@"上午",@"下午"]) {
        ItemPickerViewCellModel *cm = [[ItemPickerViewCellModel alloc] initWithModelObject:[[Item alloc] initWithName:name]];
        @MK_WEAKIFY(self);
        cm.whenSelected = ^(__kindof MKUIPickerViewCellModel * _Nonnull cellModel) {
            @MK_NORMALIZEANDNOTNIL(self);
            [self __refreshSelect];
        };
        [self.model addCellModel:cm];
    }
    //
    [self.model addSectionModel:[[MKUIPickerViewComponentModel alloc] init]];
    for (int i=0; i<6; i++) {
        NSString *name = [NSString stringWithFormat:@"%2d",i];
        Item *item = [[Item alloc] initWithName:name];
        ItemPickerViewCellModel *cm = [[ItemPickerViewCellModel alloc] initWithModelObject:item];
        @MK_WEAKIFY(self);
        cm.whenSelected = ^(__kindof MKUIPickerViewCellModel * _Nonnull cellModel) {
            @MK_NORMALIZEANDNOTNIL(self);
            [self __refreshSelect];
        };
        [self.model addCellModel:cm];
    }
    
    //
    [self.model addSectionModel:[[MKUIPickerViewComponentModel alloc] init]];
    for (int i=0; i<60; i++) {
        NSString *name = [NSString stringWithFormat:@"%2d",i];
        Item *item = [[Item alloc] initWithName:name];
        ItemPickerViewCellModel *cm = [[ItemPickerViewCellModel alloc] initWithModelObject:item];
        if(i%3==1){//不可取
            cm.cellClass = [ItemPickerViewCellNoSelect class];
            @MK_WEAKIFY(self);
            cm.whenSelected = ^(__kindof MKUIPickerViewCellModel * _Nonnull cellModel) {
                @MK_NORMALIZEANDNOTNIL(self);
                NSIndexPath *p = cellModel.indexPathInModel;
                NSInteger nextRow = p.row+1;
                if(nextRow>=[self.pickerView numberOfRowsInComponent:p.section]){
                    nextRow = 0;
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.model selectCellModelAtIndexPath:[NSIndexPath indexPathForRow:nextRow inSection:p.section] animated:YES];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self __refreshSelect];
                    });
                });
            };
        }else{
            cm.cellClass = [ItemPickerViewCell class];
            @MK_WEAKIFY(self);
            cm.whenSelected = ^(__kindof MKUIPickerViewCellModel * _Nonnull cellModel) {
                @MK_NORMALIZEANDNOTNIL(self);
                [self __refreshSelect];
            };
        }
        [self.model addCellModel:cm];
    }
    //
    [self.model addSectionModel:[[MKUIPickerViewComponentModel alloc] init]];
    for (int i=0; i<1000; i++) {
        NSString *name = [NSString stringWithFormat:@"%d",i];
        Item *item = [[Item alloc] initWithName:name];
        ItemPickerViewCellModel *cm = [[ItemPickerViewCellModel alloc] initWithModelObject:item];
        @MK_WEAKIFY(self);
        cm.whenSelected = ^(__kindof MKUIPickerViewCellModel * _Nonnull cellModel) {
            @MK_NORMALIZEANDNOTNIL(self);
            [self __refreshSelect];
        };
        [self.model addCellModel:cm];
    }
    //
    [self.model addSectionModel:[[MKUIPickerViewComponentModel alloc] init]];
    //
    [self.model reloadPickerViewData];
    //
    [self.pickerView enableCycleSelect:YES forComponent:1];
    [self.pickerView enableCycleSelect:YES forComponent:2];
    [self.pickerView enableCycleSelect:YES forComponent:3];
    [self.pickerView enableCycleSelect:YES forComponent:4];
    
    [self __refreshSelect];
}
@end
